-- UIBookInfo
-- Created by chendh Oct/20/2015
-- 日记界面

UIBookInfo = class("UIBookInfo", function()
    return cc.Layer:create();
end);

function UIBookInfo.create(classId)
    return UIBookInfo.new(classId);
end

-- 构造函数
function UIBookInfo:ctor(classId)
    self:setName("UIBookInfo");
    local node = cc.CSLoader:createNode("layout/equip/BookInfo.csb");
    self:addChild(node);
    self.node = node;
    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    local scrollview = findChildByName(self.node, "CT/book_info");
    -- 容器原本的内高
    self.oriHeight = scrollview:getInnerContainerSize().height;
    self.scrollview = scrollview;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘界面
    self:redraw(classId);

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIBookInfo:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIBookInfo");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 重绘
function UIBookInfo:redraw(classId)
    -- 标题
    local titleLabel = findChildByName(self.node, "CT/title");
    TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_ORANGE, true);
    titleLabel:setString(ItemM.query(classId, "name"));

    -- 书籍内容
    local descLabel = findChildByName(self.node, "CT/book_info/desc");
    local desc = ItemM.getItemDesc(classId);
    local dbase = PropertyM.query(classId, "dbase");

    local classify;
    local demonInfo;
    if dbase["evil_status"] then
        classify = dbase["evil_status"];
        demonInfo = EvilStatueM.getStatueInfo(classify);
    else
        classify = dbase["demon_statue"];
        demonInfo = DemonStatueM.getDemonInfo(classify);
    end
    local maxLevel = demonInfo["max_level"] or 0;
    local curLevel = demonInfo["cur_level"] or 0;
    local propDesc = "";
    local limitLevel = 100;   -- 有限制献祭等级

    for level = 1, 100 do
        local levelDes;
        if dbase["evil_status"] then
            levelDesc = EvilStatueM.getStatueBonusDesc(classify, level);
            limitLevel = EvilStatueM.getLimitLevel(classify);
        else
            levelDesc = DemonStatueM.getStatueBonusDesc(classify, level);
        end

        -- 如果没有了就跳出循环
        if not levelDesc or level > limitLevel then
            break;
        end

        if level <= curLevel then
            -- 当前已达到的等级
            if level < 10 then
                propDesc = propDesc .. "[blue]" .. string.format(getLocStr("book_prop_desc1"), level, levelDesc) .. "[-]";
            else
                propDesc = propDesc .. "[blue]" .. string.format(getLocStr("book_prop_desc2"), level, levelDesc) .. "[-]";
            end
        elseif level <= maxLevel then
            -- 当前已达到的等级
            if level < 10 then
                propDesc = propDesc .. "[gray]" .. string.format(getLocStr("book_prop_desc1"), level, levelDesc) .. "[-]";
            else
                propDesc = propDesc .. "[gray]" .. string.format(getLocStr("book_prop_desc2"), level, levelDesc) .. "[-]";
            end
        else
            -- 当前已达到的等级
            if level < 10 then
                propDesc = propDesc .. "[gray]" .. string.format(getLocStr("book_prop_desc1"), level, getLocStr("book_prop_desc3")) .. "[-]";
            else
                propDesc = propDesc .. "[gray]" .. string.format(getLocStr("book_prop_desc2"), level, getLocStr("book_prop_desc3")) .. "[-]";
            end
        end
    end

    desc = string.format("%s%s%s", desc, "\n\n", propDesc);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    -- 适应内高
    local needHeight = getRealContentSize(descLabel).height + 50;
    local totalHeight = math.max(self.oriHeight, needHeight);
    self.scrollview:setInnerContainerSize(cc.size(self.scrollview:getInnerContainerSize().width, totalHeight));
    descLabel:setPositionY(totalHeight - 10);
end

-- 适配
function UIBookInfo:resize()
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end
